import 'package:baselib/utils/color_base_util.dart';
import 'package:baselib/utils/drawable_base_util.dart';
import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/px_base_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class CommPasswordTextField extends StatefulWidget{

  final TextInputAction textInputAction;
  final Color foucosUnderlineColor;
  final Color defaultUnderlineColor;
  final TextStyle textStyle;
  final TextStyle hintTextStyle;
  final String lableText;
  final TextStyle lableTextStyle;
  final TextAlign textAlign;
  final String hintText;
  final int maxLength;
  final int maxLines;
  final double paddingBottm;
  final bool autoFocus;
  final bool isShowEye;
  final TextInputType keyboardType;
  final List<TextInputFormatter> inputFormatters;
  final Color cursorColor;
  final Function onSubmitted;
  final Function onChanged;
  final String defaultCont;
  final Widget prefixIcon;
  final Color eyeShowColor;

  CommPasswordTextField({
    this.textInputAction: TextInputAction.done,
    this.foucosUnderlineColor:const Color(0xFF0E95FD),
    this.defaultUnderlineColor:const Color(0xFFcccccc),
    this.textStyle,
    this.cursorColor,
    this.hintTextStyle,
    this.textAlign:TextAlign.start,
    this.hintText,
    this.lableText,
    this.lableTextStyle,
    this.maxLength: 20,
    this.maxLines: 1,
    this.paddingBottm:4,
    this.autoFocus: false,
    this.isShowEye: true,
    this.keyboardType: TextInputType.text,
    this.inputFormatters,

    this.onSubmitted,
    this.onChanged,
    this.defaultCont,
    this.prefixIcon,
    this.eyeShowColor,
  });

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _CommPasswordTextFieldState();
  }

}

class _CommPasswordTextFieldState extends State<CommPasswordTextField> {
  //是否可见
  bool isShow=false;

  TextEditingController _editingController;
  @override
  void initState() {
    super.initState();
    _editingController=TextEditingController(text: widget.defaultCont);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Stack(
      alignment: Alignment.centerRight,
      children: <Widget>[
        TextField(
          style: widget.textStyle,
          textAlign: widget.textAlign,
          maxLength: widget.maxLength,
          maxLines: widget.maxLines,
          obscureText: !isShow,
          autofocus: widget.autoFocus,
          controller: _editingController,
          textInputAction: widget.textInputAction,
          keyboardType: widget.keyboardType,
//          // 数字、手机号限制格式为0到9(白名单)， 密码限制不包含汉字（黑名单）
          inputFormatters:widget.inputFormatters,

//          inputFormatters: (widget.keyboardType == TextInputType.number || widget.keyboardType == TextInputType.phone) ?
//          [WhitelistingTextInputFormatter(RegExp("[0-9]"))] : [BlacklistingTextInputFormatter(RegExp("[\u4e00-\u9fa5]"))],
          cursorColor: widget.cursorColor??Colors.blue,
          onSubmitted: (val){
//            print("点击了键盘上的动作按钮，当前输入框的值为：${val}");
            if(widget.onSubmitted!=null){
              widget.onSubmitted(val);
            }
          },
          onChanged: (value){
            if(widget.onChanged!=null)widget.onChanged(value);
          },
          decoration: InputDecoration(
              prefixIcon: widget.prefixIcon,
              contentPadding: EdgeInsets.only(
                bottom: widget.paddingBottm,
              ),
              isDense: true,
              hintText: widget.hintText,
              hintStyle: widget.hintTextStyle,
              labelText:widget.lableText,
              labelStyle: widget.lableTextStyle,
              counterText: "",
              focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(
                      color: widget.foucosUnderlineColor,
                      width: 1
                  )
              ),
              enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(
                      color: widget.defaultUnderlineColor,
                      width: 1
                  )
              )
          ),
        ),
        //点击眼睛
        Visibility(
          visible: widget.isShowEye,
          child: InkWell(
            highlightColor: Colors.white,
            splashColor:  Colors.white,
            child: isShow?
            ImagesUtil.loadAssetImage(
              DrawableBaseUtil().eye_close,
              width: PxBaseUtil().px_18,
            ):
            ImagesUtil.loadAssetImage(
                DrawableBaseUtil().eye_show,
                width: PxBaseUtil().px_18,
                // color: widget.eyeShowColor??Colors.blue
            ),
            onTap: (){
              if(mounted){ //mounted 挂在后在setState
                setState(() {
                  isShow=!isShow;
                });
              }
            },
          ),
        )
      ],
    );
  }
}